//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Practices.Mobile.ConnectionMonitor.Configuration;
using Microsoft.Practices.Mobile.Configuration;
using Microsoft.Practices.Mobile.ConnectionMonitor.Implementations;

namespace Microsoft.Practices.Mobile.ConnectionMonitor
{
    public static class ConnectionMonitorFactory 
	{
		private static ConnectionMonitor monitor = new ConnectionMonitor();
		private static bool initialized;

        /// <summary>
		/// This factory method initializes the ConnectionMonitor single instance adding networks acording to the 
		/// native Connection Manager API and connections from the deafault configuration section
		/// "Connections".
        /// </summary>
		/// <returns>The ConnectionMonitor instance.</returns>
		public static ConnectionMonitor CreateFromConfiguration()
		{
			return CreateFromConfiguration("Connections");
		}

		/// <summary>
		///		This factory method initializes the ConnectionMonitor single instance adding networks acording to the 
		///		native Connection Manager API and connections from the supplied configuration section
		///		name.
		/// </summary>
		/// <param name="sectionName">
		///		Name of the section in the configuration file (App.config), wich contains the different
		///		types of connection
		/// </param>
		/// <returns>The ConnectionMonitor instance.</returns>
        public static ConnectionMonitor CreateFromConfiguration(string sectionName)
        {
			lock (monitor)
			{
				if (!initialized)
				{
					ConnectionSettingsSection configuration = ConfigurationManager.GetSection(sectionName) as ConnectionSettingsSection;

					if (configuration != null)
					{
						foreach (ConnectionItemElement itemConnection in configuration.ConnectionItems)
						{
							switch (itemConnection.Type)
							{
								case "NicConnection":
									monitor.Connections.Add(new NicConnection("NicConnection", itemConnection.Price));
									break;
								case "DesktopConnection":
									monitor.Connections.Add(new DesktopConnection("DesktopConnection", itemConnection.Price));
									break;
								case "CellConnection":
									monitor.Connections.Add(new CellConnection("CellConnection", itemConnection.Price));
									break;
								default:
									throw new ConnectionMonitorException("Unsupported Connection Type",0);
							}
						}
					}

					initialized = true;
				}
			}
			return monitor;
		}

		/// <summary>
		/// Provides access to the single instance of the ConnectionMonitor.
		/// </summary>
		public static ConnectionMonitor Instance
		{
			get 
			{
				lock (monitor)
				{
					if (!initialized)
						throw new InvalidOperationException(Properties.Resources.InstanceNotInitialized);
					return monitor;
				}
			}
		}
    }
}
